<?php
include ('../../configs/cms.php');
include ('../includes/index.php');
include ('../includes/404.php');
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
	if ($_SESSION['which'] == 'choose1')
	{
		$_SESSION['which'] = 'choose2';
		$query1 = mysql_query("SELECT `title`, `content` FROM `nieuwsbrief` WHERE `nieuwsbrief_id` = '" . $_POST['nieuwsbrief'] . "'");
		$result1 = mysql_fetch_assoc($query1);
		?>
		<script type="text/javascript" src="includes/ckeditor/ckeditor.js"></script>
		<div style="margin-left: 40px;"><form method="POST" action="">
		Titel:<br>
		<input type="text" name="title" size="20" maxlength="255" value="<?php echo $result1['title']; ?>">
		<br /><br />
		Inhoud:<br>
		<textarea class="ckeditor" name="content" id="content" rows="30" cols="100" wrap="soft"><?php echo $result1['content']; ?></textarea>
		<script type="text/javascript">
		CKEDITOR.replace( 'content' ); 

		</script>
		<br><br>
		<input type="hidden" name="nieuwsbrief_id" value="<?php echo $_POST['nieuwsbrief']; ?>">
		<input type="submit" value="Sla gewijzigde nieuwsbrief op">&nbsp;&nbsp;<input type="reset" value="Leeg formulier">
		</form></div><?php
	}
	elseif ($_SESSION['which'] == 'choose2')
	{
		$title = $_POST['title'];
		$content = $_POST['content'];
		$nieuwsbrief_id = $_POST['nieuwsbrief_id'];
		
		if (strlen($title) > 255 || empty($title))
		{
			echo 'De titel is te lang of niet ingevuld<br>
			<a href="#back" onclick="history.go(-1)">Ga terug</a> om het te wijzigen.';
		}
/* 		elseif (strlen($inhoud) > 1000 || !$inhoud)
		{
			echo 'Je hebt te veel tekst geplaatst (max 1000 tekens) of niets ingevuld<br>
			<a href="#back" onclick="history.go(-1)">Ga terug</a> om het te wijzigen.';
		} */
		else
		{
			$sql = "UPDATE `nieuwsbrief` SET `title` = '" . $title . "', `content` = '" . $content . "' WHERE `nieuwsbrief_id` = '" . $nieuwsbrief_id . "';";
			mysql_query($sql);
			echo 'De nieuwsbrief ' . $title . ' is met succes gewijzigd.';
		}
	}
}
else
{
?>
	<div style="margin-left: 40px;"><form method="POST" action="">
	Welke nieuwsbrief:<br />
	<?php
	$_SESSION['which'] = 'choose1';
	$query = mysql_query ("SELECT `nieuwsbrief_id`, `title` FROM `nieuwsbrief`;");
	echo '<select name="nieuwsbrief">';
	while ($result = mysql_fetch_assoc ($query))
	{
		echo '<option value="' . $result['nieuwsbrief_id'] . '">' . $result['title'] . '</option>' . "\n";
	}
	echo '</select>';
	?>
	<br /><br />
	<input type="submit" value="Wijzig nieuwsbrief">&nbsp;&nbsp;<input type="reset" value="Leeg formulier"></form></div>
<?php
}
?>